package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardPVERoom;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/15 17:16
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_LOAD_ROOM, desc = "加载pvp 房间 或者 领取pvp 奖励 （中途退出调用/pvp对战结束后调用）")
public class ShuiHuYeZiCardLoadRoomCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardLoadRoomReqMsg reqMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardLoadRoomReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();
        ret = ShuiHuYeZiCardMgr.canEntryActivity(player,activityId);
        if(ret != 0){
            return;
        }
        if(type == 1){
            // 加载pve
            ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
            ShuiHuYeZiCardPVERoom npcRoom = shuiHuYeZiCardModule.getNpcRoom();
            if(npcRoom == null){
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_NOT_OPEN_GAME;
                return;
            }
            ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.newBuilder();
            ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.Builder roomInfo = npcRoom.builderShuiHuYeZiCardRoomInfo(player.getUserId());
            resp.setRoomInfo(roomInfo);
            resp.setRet(0);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_LOAD_PVP_ROOM,resp);
        }else {
            player.sendPacket(CrossProtocol.C_CROSS_SHUIHUYEZICARD_LOAD_PVP_ROOM,reqMsg.toBuilder());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardLoadRoomRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardLoadRoomRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_LOAD_PVP_ROOM,resp);
        }
    }
}
